package com.lianzhihui.romeex.ui.trade.fragment;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.lianzhihui.romeex.R;
import com.lianzhihui.romeex.bean.PledgeRecordsBean;
import com.lianzhihui.romeex.config.AppConstants;
import com.lianzhihui.romeex.config.MessageEvent;
import com.lianzhihui.romeex.mvp.presenter.ExtractRecrodPresenter;
import com.lianzhihui.romeex.mvp.view.ExtractRecrodView;
import com.lianzhihui.romeex.ui.base.BaseFragment;
import com.lianzhihui.romeex.ui.trade.adapter.ExtractRecordAdapter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * ProjectName: romeex
 * Package: com.lianzhihui.romeex.ui.trade.fragment
 * ClassName: ExtractFragment
 * Description: 提取记录
 * Author: zhanghong
 * CreateDate: 2020/10/30 14:39
 * UpdateUser: 更新者：
 * UpdateDate: 2020/10/30 14:39
 * UpdateRemark: 更新说明：
 * Version: 1.0
 */
public class ExtractFragment extends BaseFragment implements ExtractRecrodView, OnLoadMoreListener {

    @BindView(R.id.refresher)
    SmartRefreshLayout refresher;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;

    ExtractRecordAdapter adapter;
    ExtractRecrodPresenter presenter;
    ArrayList<PledgeRecordsBean.PledgeRecordBean> list = new ArrayList<>();
    String status = "-1";

    public void setStatus(String status) {
        this.status = status;
        pageNum = 1;
        getData();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_extract;
    }

    @Override
    protected void initView() {
        presenter = new ExtractRecrodPresenter(activity, this);
        refresher.setOnLoadMoreListener(this);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        adapter = new ExtractRecordAdapter(getActivity(), list);
        recyclerView.setAdapter(adapter);
        adapter.setEmptyView(getEmptyView());
        getData();
    }

    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
        getData();
    }

    public void getData() {
        presenter.getExtranct(pageNum, status);
    }

    @Override
    public void onExtractRecordSuccess(List<PledgeRecordsBean.PledgeRecordBean> content) {
        EventBus.getDefault().post(new MessageEvent(AppConstants.Event.END_REFRESH_FLOWCELL,true));
        refresher.finishLoadMore();
        if (pageNum == 1) {
            adapter.setNewData(content);
        } else {
            adapter.addData(content);
        }
        if (content != null && content.size() > 0) {
            pageNum++;
        }
    }

    @Override
    public void onExtractRecordFail(String message) {
        EventBus.getDefault().post(new MessageEvent(AppConstants.Event.END_REFRESH_FLOWCELL,true));
        refresher.finishLoadMore();
    }

    public void refresh(){
        pageNum = 1;
        getData();
    }

}